package com.focusai.efairy.ui.adapter.report;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.focusai.efairy.R;
import com.focusai.efairy.model.dev.DevType;
import com.focusai.efairy.model.report.AlarmScaleMapEntity;
import com.focusai.efairy.utils.PrUtils;
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.PercentFormatter;

import java.util.ArrayList;
import java.util.List;

import me.drakeet.multitype.ItemViewBinder;

/**
 * 报警饼形图
 * Created by luo_guangwen on 2017/8/1.
 */

public class ReportArcViewBinder extends ItemViewBinder<AlarmScaleMapEntity, ReportArcViewBinder.ViewHolder> {

    private int[] colors;
    private Context mContext;

    public ReportArcViewBinder(int[] colors, Context mContext) {
        this.colors = colors;
        this.mContext = mContext;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_report_arc, parent, false);
        ViewHolder viewHolder = new ViewHolder(root);
        initPieChart(viewHolder.pieChart);
        return viewHolder;
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull AlarmScaleMapEntity item) {
        holder.pieChart.setData(refreshPieChart(item.scale_map_list));
        holder.pieChart.invalidate();
        holder.pieChart.animateY(1500, Easing.EasingOption.EaseInOutQuad);
        holder.titleText.setText(DevType.getDevTypeNameById(10,item.device_type));
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        PieChart pieChart;
        TextView titleText;
        ViewHolder(View itemView) {
            super(itemView);
            pieChart = (PieChart) itemView.findViewById(R.id.pie_chart);
            titleText = (TextView) itemView.findViewById(R.id.text_title);
        }
    }

    private void initPieChart(PieChart mChart) {
//        mChart.getLegend().setEnabled(false);
//        mChart.setCenterText("");
//        mChart.setDrawCenterText(false);  //饼状图中间可以添加文字
//        mChart.setDrawHoleEnabled(false); //实心饼图
//        mChart.setDrawEntryLabels(false);  //显示扇形名称
//        mChart.setCenterTextSizePixels(mChart.getContext().getResources().getDimensionPixelSize(R.dimen.font_14));
//        mChart.setDescription(null);
//        mChart.setCenterTextColor(mChart.getContext().getResources().getColor(R.color.font_gray));
//        mChart.setCenterTextTypeface(Typeface.DEFAULT_BOLD);
//
//        mChart.setUsePercentValues(true);
//        mChart.setDrawSliceText(false);
//        mChart.setRotationEnabled(true); // 可以手动旋转
//
////        mChart.getDescription().setEnabled(false);
//        mChart.setExtraOffsets(5, 10, 5, 5);
//        mChart.setHoleColor(Color.WHITE);
//
//        mChart.setTransparentCircleColor(Color.WHITE);
//        mChart.setTransparentCircleAlpha(110);
//
//        mChart.setHoleRadius(58f);
//        mChart.setTransparentCircleRadius(61f);
//
//
//        mChart.setRotationAngle(0);
//        // enable rotation of the chart by touch
//        mChart.setHighlightPerTapEnabled(true);
//
//        // mChart.setUnit(" €");
//        // mChart.setDrawUnitsInChart(true);
//
//        // add a selection listener
//
//        mChart.animateY(1400, Easing.EasingOption.EaseInOutQuad);
//        // mChart.spin(2000, 0, 360);
//
//        Legend l = mChart.getLegend();
//        l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
//        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
//        l.setOrientation(Legend.LegendOrientation.VERTICAL);
//        l.setDrawInside(false);
//        l.setXEntrySpace(7f);
//        l.setYEntrySpace(0f);
//        l.setYOffset(0f);
//
//        // entry label styling
//        mChart.setEntryLabelColor(Color.WHITE);
//        mChart.setEntryLabelTextSize(12f);
        mChart.setUsePercentValues(true);
        mChart.getDescription().setEnabled(false);
        mChart.setExtraOffsets(5, 10, 5, 5);
        mChart.setDrawHoleEnabled(false); //实心饼图

        mChart.setDragDecelerationFrictionCoef(0.95f);

        mChart.setCenterText("");

        mChart.setHoleColor(Color.WHITE);

        mChart.setTransparentCircleColor(Color.WHITE);
        mChart.setTransparentCircleAlpha(110);

        mChart.setHoleRadius(58f);
        mChart.setTransparentCircleRadius(61f);

        mChart.setDrawCenterText(true);

        mChart.setRotationAngle(0);
        // enable rotation of the chart by touch
        mChart.setRotationEnabled(true);
        mChart.setHighlightPerTapEnabled(true);

        // mChart.setUnit(" €");
        // mChart.setDrawUnitsInChart(true);

        // add a selection listener

        // mChart.spin(2000, 0, 360);

        Legend l = mChart.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
        l.setOrientation(Legend.LegendOrientation.VERTICAL);
        l.setDrawInside(false);
        l.setXEntrySpace(7f);
        l.setYEntrySpace(0f);
        l.setYOffset(0f);

        // entry label styling
        mChart.setDrawEntryLabels(true);
        mChart.setEntryLabelColor(Color.WHITE);
        mChart.setEntryLabelTextSize(12f);
    }

    /**
     * 刷新饼图
     */
    private PieData refreshPieChart(List<AlarmScaleMapEntity.ScaleMapItem> list) {
        if (list == null)
            list = new ArrayList<>();
        ArrayList<PieEntry> yVals = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            //屏蔽为0的资金账户
            AlarmScaleMapEntity.ScaleMapItem entity = list.get(i);
            if (entity.alarm_times != 0) {
                //小于0的不展示
                yVals.add(new PieEntry(entity.alarm_times, PrUtils.getNameByPt(entity.pt)));
            }
        }
        PieDataSet dataSet = new PieDataSet(yVals, "");

        dataSet.setDrawIcons(false);
        dataSet.setColors(colors,mContext);
        dataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
        dataSet.setValueLineColor(mContext.getResources().getColor(R.color.font_gray));

        PieData data = new PieData(dataSet);
        data.setValueFormatter(new PercentFormatter());
        data.setValueTextColor(mContext.getResources().getColor(R.color.font_gray));
        data.setValueTextSize(mContext.getResources().getDimensionPixelSize(R.dimen.font_10));
        return data;
    }
}

